package org.brisling.sysman.queryService;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.lang3.StringUtils;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.sysman.domain.SysDictionary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/dictionary/query")
public class QueryDictionaryService extends AbstractBaseQueryService<SysDictionary>{
	
		
	public QueryDictionaryService(){
		this.moduleName = ConstSysParam.MODULE_DICTIONARY;
		this.subSystem = ConstSysParam.SUBSYSTEM_SYSMAN;
		this.tableName = this.getTableName(SysDictionary.class);
		this.clazz = SysDictionary.class;
		
		typeMap = this.getTypeMap(clazz);
		
		orderMap.put("sortedid","desc");			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<SysDictionary> getListDomain(@RequestBody(required=false) String body){	
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
		
	}
	
	
	@RequestMapping("/getTreeList.action")
	public List<SysDictionary> getTreeList(@RequestParam(value="id",required=false) Long id){
		
		List<SysDictionary> diclists =  SysConfig.getDicTree(id);		
		
		if(diclists!=null && diclists.size()>0){
			return diclists;
		}else{
			diclists = this.getPagedTreeList(id);
			SysConfig.setDicTree(id, diclists);
			return diclists;
		}
		
	}
	
	
	@RequestMapping("/getList.action")
	public List<SysDictionary> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public SysDictionary getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public SysDictionary getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	
	
	@RequestMapping("/getOneByField.action")
	public SysDictionary getOneByField(@RequestParam(value="field",required=false) String field,@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getOneByIdentify(field,fieldVal);
	}
	
	@RequestMapping("/getSqlByBizcode.action")
	public String getSqlByBizcode(@RequestParam(value="bizcode",required=false) String bizcode){	
		String retVal = null;
		
		if(bizcode!=null && bizcode.trim().length()>0){
			bizcode = bizcode.trim();
			retVal = SysConfig.getBizidSql(bizcode);
		}
		
		if(retVal==null || retVal.trim().length()<=0){
			SysDictionary dic = super.getOneByIdentify("bizcode",bizcode);
			if(dic!=null){
				retVal = dic.getRemarks();
				if(retVal != null){
					retVal = StringUtils.normalizeSpace(retVal);
					SysConfig.setBizidSql(bizcode, retVal);
					return retVal;
				}
			}
			return null;
			
		}else{
			return retVal;
		}
	}
	
	@RequestMapping("/reloadCache.action")
	public RetDomain reloadCache(){
		RetDomain domain = new RetDomain();		
		
		SysConfig.refreshDicParam();;
		
		domain.setSuccessMsg("success");
		return domain;
	}
	
	
}
